package com.dzt.cloudreader.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dzt.cloudreader.utils.DialogMaker;
import com.dzt.cloudreader.utils.JPreferenceKit;
import com.dzt.cloudreader.utils.JToastKit;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by M02323 on 2017/12/15.
 */

public abstract class SuperFragment extends Fragment
		implements DialogMaker.DialogCallBack {
	protected Unbinder unbinder;
	protected Activity context;
	protected Bundle savedInstanceState;
	protected Dialog dialog;
	protected View view;
	protected JPreferenceKit preferenceKit;
	private CompositeSubscription mCompositeSubscription;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = getActivity();
		this.savedInstanceState = savedInstanceState;
		preferenceKit = JPreferenceKit.getInstance();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		view = inflater.inflate(getLayoutId(), null);
		unbinder = ButterKnife.bind(this, view);
		initWidgets();
		return view;
	}

	protected abstract int getLayoutId();

	protected abstract void initWidgets();

	@Override
	public void onBtnClicked(Dialog dialog, int position, Object tag) {

	}

	/**
	 * pop dialog
	 *
	 * @param title
	 * @param msg
	 * @param btns
	 * @param isCanCancelabel
	 * @param isDismissAfterClickBtn
	 * @return
	 */
	public Dialog showAlertDialog(String title, String msg, String[] btns,
								  boolean isCanCancelabel, final boolean isDismissAfterClickBtn,
								  Object tag) {
		if (null == dialog || !dialog.isShowing()) {
			DialogMaker.showCommonAlertDialog(context, title, msg, btns, null,
					isCanCancelabel, isDismissAfterClickBtn, tag);
		}
		return dialog;
	}

	public Dialog showWaitDialog(String msg, boolean isCanCancelabel, Object tag) {
		if (null == dialog || !dialog.isShowing()) {
			dialog = DialogMaker.showCommonWaitDialog(context, msg, null,
					isCanCancelabel, tag);
		}
		return dialog;
	}

	protected void showToast(String msg) {
		JToastKit.getInstance().showMessage(msg);
	}

	/**
	 * close dialog
	 */
	public void dismissDialog() {
		if (null != dialog && dialog.isShowing()) {
			dialog.dismiss();
		}
	}

	public void addSubscription(Subscription s) {
		if (mCompositeSubscription == null) {
			mCompositeSubscription = new CompositeSubscription();
		}
		mCompositeSubscription.add(s);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		unbinder.unbind();
		dismissDialog();
		if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
			mCompositeSubscription.unsubscribe();
		}
	}

	protected void startActivity(Class<?> cls, Intent intent) {
		if (null == intent) {
			intent = new Intent(context, cls);
		} else {
			intent.setClass(context, cls);
		}
		startActivity(intent);
	}
}